//
//  YCLEmoticonPackageMode.swift
//  02.表情键盘
//
//  Created by yaochunlin on 16/4/25.
//  Copyright © 2016年 yaochunlin. All rights reserved.
//

import UIKit

//表情包模型
class YCLEmoticonPackageMode: NSObject {
    
    //属性
    //表情包文件夹名称
    var id: String
    //表情包名称
    var group_name_cn: String
    //表情数组
    var emoticons:[YCLEmoticonModel]
    
    //分页后的表情数组
    var pageEmoticons:[[YCLEmoticonModel]] = [[YCLEmoticonModel]]()
    
    
    init(id: String, group_name_cn:String, emoticons:[YCLEmoticonModel]) {
        
        self.id = id
        self.group_name_cn = group_name_cn
        self.emoticons = emoticons
        
        super.init()
        
        self.spliteEmoticons()
    }
    
    //对象打印方法
    override var description: String{
    
        return "表情包模型 id:\(id), group_name_cn:\(group_name_cn), emoticons:\(emoticons)"
    }
    
    //将表情模型数组分页
    private func spliteEmoticons() {
    
        // 计算总页数: 总个数 + 一页个数 - 1 / 一页个数
        let pageCount = (emoticons.count + YCLEmoticonNumberOfPage - 1) / YCLEmoticonNumberOfPage
        
        //如果一页表情都没有
        if pageCount == 0 {
            
            //创建一页空的表情
            let emptyEmoticon = [YCLEmoticonModel]()
            pageEmoticons.append(emptyEmoticon)
            return
        }
        
        for i in 0..<pageCount {
            
            let location = i * YCLEmoticonNumberOfPage
            
            var length = YCLEmoticonNumberOfPage
            
            //当最后一页起始值加没页数量 > 表情总数，说明越界
            if location + YCLEmoticonNumberOfPage > emoticons.count {
                
                 length = emoticons.count - location
            }
            
            let range = NSRange.init(location: location, length: length)
            
            //一页表情
            let spliteEmoticons = (emoticons as NSArray).subarrayWithRange(range) as! [YCLEmoticonModel]
            
            pageEmoticons.append(spliteEmoticons)
        }
        
    }
    
    
    
    

}
